<template>
	<view class="uni-input-num">
		<view class="uni-timesec">
			<span class="uni-button-b" @tap="stopTime()">暂停</span>
			<span>{{hour}}</span>:<span>{{min}}</span>:<span>{{sec}}</span>
			<span class="uni-button-b" @tap="reStartTime()">继续</span>
			</view>
		<view v-for="(item,index) in title" :key="index" class="uni-title">{{item.title}}</view>
		<view class="uni-errTitle">本次答题记录：<span>查看历史记录需<span @tap="login()" class="uni-login">登录</span></span>
		<view class="uni-errBody" v-for="(item,index) in resultList" :key="index">
			<view :style="{color:item.result === item.trueResult?'green':'red'}">
				题目：{{item.tit}} 答案：{{item.result}} 耗时：{{item.hour}}时{{item.min}}分{{item.sec}}秒</view>
			</view>
		</view>
		<view class="uni-result">请输入答案：{{uresult}}</view>
		<view class="uni-borader">
			<view class="uni-marginTop-small ">
				<button @tap="addNum(1)" class="uni-btn-larger">1</button>
				<button @tap="addNum(2)" class="uni-btn-larger">2</button>
				<button @tap="addNum(3)" class="uni-btn-larger">3</button>
			</view>
			<view class="uni-marginTop-small">
				<button @tap="addNum(4)" class="uni-btn-larger">4</button>
				<button @tap="addNum(5)" class="uni-btn-larger">5</button>
				<button @tap="addNum(6)" class="uni-btn-larger">6</button>
			</view>
			<view class="uni-marginTop-small">
				<button @tap="addNum(7)" class="uni-btn-larger">7</button>
				<button @tap="addNum(8)" class="uni-btn-larger">8</button>
				<button @tap="addNum(9)" class="uni-btn-larger">9</button>
			</view>
			<view class="uni-marginTop-small">
				<button @tap="upNum()" class="uni-badge-success">提交答案</button>
				<button @tap="addNum(0)" class="uni-btn-larger">0</button>
				<button @tap="delNum()" class="uni-badge-warning">删除答案</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {saveEasyNum} from '@/api/work/study.js'
	export default {
		name:"numInput",
		data() {
			return {
			timeStatus:'false',
			hour:'00',
			min:'00',
			sec:'00',
			uresult:'',
			resultList:[],
			timer:{},
			nowResult:{},
			saveResult:[],
			i: 0,
			};
		},
		props:['title'],
		mounted() {
			this.startTime()
		},

		methods:{
			stopTime(){
				if(this.timeStatus == 'false'){
					clearInterval(this.timer)
					this.timeStatus='true'
				}
			},
			reStartTime(){
				if(this.timeStatus == 'true'){
					this.timer = setInterval(this.start,1000)
					this.timeStatus='false'
				}
			},
			login(){
				uni.navigateTo({
					url:'/pages/login'
				})
			},
			startTime(){
				clearInterval(this.timer)		
				this.timer = setInterval(this.start,1000)
			},
			start(){
				this.i++
				this.sec = this.doubleNum(this.i%60)
				this.min = this.doubleNum(parseInt(this.i / 60) %60)
				this.hour = this.doubleNum(parseInt(this.i / 3600))
			},
			doubleNum(n){
				if(n < 10){
				   return "0" + n;
				}else{
				    return n;
				}
			},
			upNum(){				
				if(this.uresult != ''){
					let status = this.$store.state.tableStatus;   // 重新赋值
					let trueResult = 0
				    this.$store.commit("changeStatus", !status);
					 this.nowResult ={
						tit : this.title[0].title,
						result : this.uresult*1.0,
						hour : this.hour,
						min : this.min,
						sec : this.sec,
						trueResult : this.title[0].result*1.0
					}
					this.saveResult = {
						title : this.title[0].title,
						result : this.uresult,
						remark : '耗时:'+this.hour+'时'+this.min+'分'+this.sec+'秒',
						statu: this.uresult == this.nowResult.trueResult? 1:0
					}
					saveEasyNum(this.saveResult).then(resp=>{
						
					})
					this.resultList.push(this.nowResult)
					this.i= 0
					this.sec= '00' 
					this.min= '00' 
					this.hour= '00'
					this.uresult = ''
					this.startTime()
				}

			},
			addNum(val){
				this.uresult = this.uresult + val +''		
			},
			
			delNum(){
				this.uresult = Math.floor( this.uresult / 10.0)
			}
		},
		}
</script>

<style lang="scss">

</style>